package cn.edu.yzu.scholarship.model;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.math.NumberUtils;

import cn.edu.yzu.scholarship.constant.C;
import cn.edu.yzu.scholarship.kit.Sak;
import cn.edu.yzu.scholarship.pojo.Pager;

import com.jfinal.ext.plugin.sqlinxml.SqlKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Record;

public class Grant extends Model<Grant> {

	private static final long serialVersionUID = -2400738878162296258L;
	
	public static final Grant dao = new Grant();
	
	private static final int DEFAULT_YEAR_LENGTH = 5;
	
	public static List<Map<String,Object>> getYears() {
		int thisYear = Calendar.getInstance().get(Calendar.YEAR);
		List<Map<String,Object>> list = Sak.newArrayList();
		for(int i = thisYear ; i > thisYear - DEFAULT_YEAR_LENGTH; i--) {
			Map<String,Object> map = Sak.newHashMap();
			map.put("id", String.valueOf(i).substring(2));//取最后两位
			map.put("value", i);
			list.add(map);
		}
		return list;
	}

	public static List<Map<String,Object>> getClassifys() {
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		
		Map<String,Object> map1 = Sak.newHashMap();
		map1.put("id", "M");
		map1.put("value", "学术硕士");
		
		Map<String,Object> map2 = Sak.newHashMap();
		map2.put("id", "MZ");
		map2.put("value", "专业硕士");
		
		Map<String,Object> map3 = Sak.newHashMap();
		map3.put("id", "N");
		map3.put("value", "农业硕士");
		
		list.add(map1);
		list.add(map2);
		list.add(map3);
		
		return list;
	}
	
	public static void recordGrant(Record teacher,String title,String filePath, List<Map<String,Object>> data) {
		double money = 0;
		
		for(Map<String,Object> map : data) {
			money += NumberUtils.toDouble(String.valueOf(map.get("scholarshipMoney")), 0);
		}
		Db.update(SqlKit.sql("grant.insertMain"),filePath,money,data.size(),teacher.get("id"),teacher.get("name"),title);
		int grantId = Db.findFirst(SqlKit.sql("grant.findMainByFilePath"),filePath).getInt("id");
		
		Object[][] paras = new Object[data.size()][];
		for(int i = 0; i < data.size(); i++) {
			Map<String,Object> map = data.get(i);
			paras[i] = new Object[]{grantId,map.get("xh"),map.get("xm"),map.get("scholarshipMoney"),
					map.get("scholarshipLevel"),map.get("levelName"),map.get("scholarshipId"),
					map.get("scholarshipName"),map.get("lqyxsm"),map.get("lqyxsmc")};
		}
		Db.batch(SqlKit.sql("grant.insertOne"), paras, 200);
	}
	
	public static Pager<Record> paginate(int pageNumber,int pageSize,Map<String,String> params) {
		String startdate = params.get("startdate");
		String sql1 = C.PARAM_ALL.equals(startdate) ? "" : "and date_format(grantTime,'%Y-%m-%d') >= '" + startdate + "' ";
		
		String enddate = params.get("enddate");
		String sql2 = C.PARAM_ALL.equals(enddate) ? "" : "and date_format(grantTime,'%Y-%m-%d') <= '" + enddate + "' ";
		
		String xh = params.get("xh");
		String sql3 = C.PARAM_ALL.equals(xh) ? "" : "and (xh = '" + xh + "' or xm like '%" + xh + "%') ";
		
		String lqyxsm = params.get("lqyxsm");
		String sql4 = C.PARAM_ALL.equals(lqyxsm)? "" : "and lqyxsm = '" + lqyxsm + "' ";
		
		String scholarshipId = params.get("scholarshipId");
		String sql5 = C.PARAM_ALL.equals(scholarshipId) ? "" : "and scholarshipId = '" + scholarshipId + "' ";
		
		return Pager.toPager(Db.paginate(pageNumber, pageSize, "select * ", " from grantDetail where 1 = 1 " + sql1 + sql2 + sql3 + sql4 + sql5 + " order by grantTime desc"));
	}

	public static Pager<Record> paginateFiles(int pageNumber, int pageSize) {
		return Pager.toPager(Db.paginate(pageNumber, pageSize, "select * ", " from `grant` order by grantTime desc"));
	}

}
